package com.tensquare.user.service;

import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
@Transactional
public class UserService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RabbitTemplate rabbitTemplate;


    public void sendSms(String mobile) {
        //生成6位数
        String randomNumeric = RandomStringUtils.randomNumeric(6);
        //向缓存放一份
        redisTemplate.opsForValue().set("randomNumeric"+mobile,randomNumeric,6, TimeUnit.HOURS);
        //给用户一份
        Map<String,String> map=new HashMap<>();
        map.put("mobile",mobile);
        map.put("checkcode",randomNumeric);
        rabbitTemplate.convertAndSend("sms",map);

        //在控制台
        System.out.println("验证码为:" + randomNumeric);
    }
}
